Ext.onReady(function(){ 

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var record_start = 0; 

var sm = new Ext.grid.CheckboxSelectionModel();
var cm=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",
width:40, 
renderer:function(value,metadata,record,rowIndex){ 
return record_start + 1 + rowIndex; 
} 
}),
{header:'权限流水号',dataIndex:'rightId',width:100,hidden:true},
{header:'权限名称',dataIndex:'rightName',width:100},
{header:'权限说明',dataIndex:'rightDesc',width:100}, 
{header:'登记人',dataIndex:'inputUserId',width:100},
{header:'登记人所在机构',dataIndex:'inputOrgId',width:100},
{header:'登记时间',dataIndex:'inputDate',width:100},
{header:'更新人',dataIndex:'updateUserId',width:100},
{header:'更新人所在机构',dataIndex:'updateOrgId',width:100},
{header:'更新时间',dataIndex:'updateDate',width:100},

]); 

var ds = new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'system/listRight.do',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ 
totalProperty:'total',  
root:'resultData'},[
{name:'rightId'}, 
{name:'rightName'},  
{name:'rightDesc'},
{name:'inputUserId'},
{name:'inputOrgId'},
{name:'inputDate'},
{name:'updateUserId'},
{name:'updateOrgId'},
{name:'updateDate'},
]) 
}); 
ds.load({params:{start:0,limit:20}}); 

var grid=new Ext.grid.GridPanel({ 
id:'myCustomers',
renderTo:"customer_list", 
stripeRows:true,// 斑马线效果
title:"权限列表", 
height:500, 
cm:cm,
store: ds, 
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 20,   
                store: ds,
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据" ,
                doLoad : function(start){ 
						 	record_start = start; 
							var o = {}, pn = this.getParams(); 
						    o[pn.start] = start; 
						    o[pn.limit] = this.pageSize; 
							this.store.load({params:o}); 
     			}   
            }),
 tbar: [
 {text:'新增', handler: addModel},
 '-',  
 {text:'查看/编辑', handler: modelDetail},
 '-',
 {text:'删除', handler: deleteModel},
 '-',
]              
});

// 新增权限
addForm = new Ext.form.FormPanel({ 
id: 'addModelForm', 
width: 430, 
height: 220, 
// 样式
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 80, 
items: 
[ { xtype: 'fieldset', 
title: '权限信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[ { 
xtype: 'textfield', 
	id:'rightName',
	name: 'rightName', 
	fieldLabel: '权限名称', 
	allowBlank: false, 
	emptyText: '必填', 
	blankText: '权限名称不能为空', 
width: 280,
},{ 
	xtype: 'textarea', 
	id:'rightDesc',
	name: 'rightDesc', 
	fieldLabel: '权限描述', 
	width: 280,
},
{ layout : "form",
    items : [{
xtype: 'hidden', 
id : "rightId",
       fieldLabel: '权限ID',
       allowBlank: true
}]}
] 
}]
});

// 新增权限窗口
addWin = new Ext.Window({ 
id: 'addModelWin', 
title: '权限管理', 
width: 450, 
height: 230, 
// 背景遮罩
modal: true, 
// 重置大小
resizable: false, 
// 当关闭按钮被点击时执行的动作
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addForm, 
buttons: 
[  
{ text: '保存', handler: addModel } ,
{ text: '关闭', handler: function() { Ext.getCmp('addModelWin').hide(); } }

] 
});

function addModel(){
	Ext.getCmp('addModelForm').form.reset();
	Ext.getCmp("addModelWin").buttons[0].handler = addFunction; 
	Ext.getCmp("addModelWin").show();
}

function addFunction() {
	if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 
                    url : 'system/addRight.do', 
                    method : 'post',   
                    success : function() { 		                       
                    	grid.store.reload();
                    	Ext.Msg.alert('提示', '保存成功！'); 
 
                       }, 
                        failure : function(form, action) { 
                           Ext.Msg.alert('提示', '保存未成功！'); 
                        } 
                    });  
                    }
}

function modelDetail(){
	if (grid.getSelectionModel().getSelections()[0] == undefined) { 
		Ext.Msg.alert("提示", "请选中要查看的项！"); 
		}
		else{
				var rightId = grid.getSelectionModel().getSelections()[0].data.rightId;
			
			Ext.getCmp('addModelForm').form.reset(); 
			Ext.getCmp("addModelWin").buttons[0].handler = editAddressFunction; 
			
		Ext.Ajax.request({
		url:'system/getRight.do',
		params : { rightId: grid.getSelectionModel().getSelections()[0].data.rightId},
		method:'POST',
		   success:function(response){
			var addJSON = Ext.util.JSON.decode(response.responseText);

		 	Ext.getCmp("addModelWin").show();
		  	Ext.getCmp("rightName").setValue(addJSON.rightName);
		  	Ext.getCmp("rightDesc").setValue(addJSON.rightDesc);		
		   	
		  },failure:function(){
		    Ext.MessageBox.alert("请求异常！");
		  }
		}); 
		}
}

//编辑
function editAddressFunction(){
	
	var addForm = Ext.getCmp("addModelForm");

	if(addForm.form.isValid()){
		 addForm.form.doAction('submit', { 

	                    url : 'system/editRight.do', 
	                    params : { rightId: grid.getSelectionModel().getSelections()[0].data.rightId},
	                    method : 'post', 
        
	                       success : function() { 
                   
	                    grid.store.reload();
	                       Ext.Msg.alert('提示', '保存成功！'); 
	 
	                       }, 
	                        failure : function(form, action) { 

	                           Ext.Msg.alert('提示', '保存失败！'); 
	 
	                        } 
	                    }); 
	                    }
}

function deleteModel(){
	if (grid.getSelectionModel().getSelections()[0] == undefined) { 
		Ext.Msg.alert("提示", "请选中要操作的项！"); 
	}
	else{
		var selectedSerialNo = grid.getSelectionModel().getSelections()[0].data.rightId;
		//alert(selectedSerialNo);
		//window.location.href="rating/showHistoryPDList.do?custId="+selectedCustId;
		Ext.Msg.confirm('提示','确实要删除所选中项吗？',function(bool){
			if(bool=='yes'){
				Ext.Ajax.request({
					  url:'system/deleteRight.do',
					  params : {rightId : selectedSerialNo},
					  method:'POST',
					  success:function(){
					   Ext.MessageBox.alert("提示","删除成功！");
					   grid.store.reload(); 
					   treePanel.root.reload();
					  },failure:function(){
					    Ext.MessageBox.alert("提示", "删除失败！"); 
					  }
					});
				return true;
			}
			else{
				return false;	
			}
		}); 
		
	}
}



function rowdblclickFn(grid, rowIndex, e){// 双击事件
	var row = grid.getStore().getAt(rowIndex); 
	var selectedModelId = row.get("modelId");
		window.location.href="model/viewDetail.do?modelId="+selectedModelId;
}   

grid.addListener('rowdblclick',modelDetail);

}); 